package com.sxpi.user.service.weixin;

import com.sxpi.user.pojo.User;
import com.sxpi.user.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.hutool.core.util.StrUtil;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.iai.v20200303.IaiClient;
import com.tencentcloudapi.iai.v20200303.models.CreatePersonRequest;
import com.tencentcloudapi.iai.v20200303.models.CreatePersonResponse;
import com.tencentcloudapi.iai.v20200303.models.VerifyFaceRequest;
import com.tencentcloudapi.iai.v20200303.models.VerifyFaceResponse;
import org.springframework.stereotype.Service;

/**
 * @Author: 董伟豪
 * @Date: 2022/10/10 12:44
 */
@Service
public class TecentFaceService {

    @Autowired
    private IUserService userService;

    /**
     * 创建人脸识别
     * @param userId
     * @param photo
     * @return
     */
    public String createUserFaceModel(long userId, String photo) {
        // 登录获取用户信息 -- 实名认证 -- 银行卡，身份证的识别。
        User user = userService.getById(userId);
        // 获取用户的姓名
        String name = user.getNickname();
        String sex = user.getSex() + "";
        // 创建一个人脸识别的认证对象
        Credential cred = new Credential("AKIDoX9c094mIdhIm6D7oGTTHtHQM6g398zi", "fAUec5c0FICziKZhfWDkAPR7d1EH7k9R");
        // 然后指定你的区域
        IaiClient client = new IaiClient(cred, "ap-guangzhou");
        try {
            // 开始创建人员建档
            CreatePersonRequest req = new CreatePersonRequest();
            // 人员库
            req.setGroupId("sxpi");
            // 设置用户姓名
            req.setPersonName(name);
            // 人员ID
            req.setPersonId(userId + "");
            // 性别
            req.setGender(Long.parseLong(sex));
            // 设置质量
            req.setQualityControl(4L);
            req.setUniquePersonControl(4L);
            // 设置图片(我使用的是base64)
            req.setImage(photo);
            // 人脸识别建档
            CreatePersonResponse resp = client.CreatePerson(req);
            // 如果建档成功就返回一个faceId
            if (StrUtil.isNotBlank(resp.getFaceId())) {
                User updateUser = new User();
                updateUser.setId(userId);
                updateUser.setArchive(1);
                userService.updateById(updateUser);
                return "success";
            } else {
                return "人脸识别失败";
            }
        } catch (Exception e) {
            return "请把人脸放入到圆框中";
        }
    }

    /**
     * 创建人脸识别
     * @param userId
     * @param photo
     * @return
     */
    public String verifyUserFaceModel(long userId, String photo) {
        // 获取人脸识别的认证对象
        Credential cred = new Credential("AKIDoX9c094mIdhIm6D7oGTTHtHQM6g398zi", "fAUec5c0FICziKZhfWDkAPR7d1EH7k9R");
        // 指定人识别的区域
        IaiClient client = new IaiClient(cred, "ap-guangzhou");
        try {
            // 验证请求
            VerifyFaceRequest req = new VerifyFaceRequest();
            // 人员ID
            req.setPersonId(userId + "");
            // 设置质量
            req.setQualityControl(4L);
            // 设置图片是否旋转 0 不支持 1支持
            req.setNeedRotateDetection(0L);
            // 设置图片
            req.setImage(photo);
            // 验证返回
            VerifyFaceResponse resp = client.VerifyFace(req);
            if (resp!=null &&resp.getIsMatch()) {
                return "success";
            }else{
                return "人脸识别失败";
            }
        } catch (Exception e) {
            return "请把人脸放入到圆框中";
        }
    }
}
